package read.prjx.dst;

import read.prjx.dst.data.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Dst {
    private DstData dstData;

    public DstData getDstData() {
        return dstData;
    }

    public void setDstData(String dstStr) {
        DstData dstDat = new DstData();
        PatTenr patTenr = new PatTenr();
        ScheMe scheMe = new ScheMe();
        BreAkln breAkln = new BreAkln();

        List<String> dstStrList = Arrays.asList(dstStr.split("@"));

        String[] patStrList = dstStrList.get(0).split(System.getProperty("line.separator"));
        for (String str : patStrList) {
            if (str.contains("GID")) {
                patTenr.setgId(str.split("=", 2)[1]);
            }
            if (str.contains("GDESCRIPTION")) {
                patTenr.setGdeScrIpTion(str.split("=", 2)[1]);
            }
            if (str.contains("GSTRUCTURED")) {
                patTenr.setGstRucTured(str.split("=", 2)[1]);
            }
            if (str.contains("GCOATED")) {
                patTenr.setGcoAted(str.split("=", 2)[1]);
            }
            if (str.contains("REPEAT")) {
                patTenr.setRePeat(str.split("=", 2)[1]);
            }
            if (str.contains("GCLR")) {
                patTenr.setGcLr(str.split("=", 2)[1]);
            }
            if (str.contains("GTHICKNESS")) {
                patTenr.setGthiCkness(str.split("=", 2)[1]);
            }
            if (str.contains("WIDTH")) {
                patTenr.setWidTh(str.split("=", 2)[1]);
            }
            if (str.contains("HEIGHT")) {
                patTenr.setHeigHt(str.split("=", 2)[1]);
            }
            if (str.contains("TRIMLEFT")) {
                patTenr.setTrimLeft(str.split("=", 2)[1]);
            }
            if (str.contains("TRIMBOTTOM")) {
                patTenr.setTriMbotTom(str.split("=", 2)[1]);
            }
            if (str.contains("BORDERTOP")) {
                patTenr.setBordeRtop(str.split("=", 2)[1]);
            }
            if (str.contains("BORDERRIGHT")) {
                patTenr.setBordeRrIght(str.split("=", 2)[1]);
            }
        }

        String[] schemeStrList = dstStrList.get(1).split(System.getProperty("line.separator"));
        List<String> xyList = new ArrayList<>();
        List<String> ibList = new ArrayList<>();
        List<String> dbList = new ArrayList<>();
        for (String str : schemeStrList) {

            if (!str.equals("") && !str.contains("SCHEME")) {
                String[] strArr = str.trim().split(" ");

                if (strArr.length == 1) {
                    xyList.add(strArr[0]);
                }
                if (strArr.length == 2) {
                    xyList.add(strArr[0]);
                    ibList.add(strArr[1]);
                }
                if (strArr.length == 3) {
                    xyList.add(strArr[0]);
                    dbList.add(strArr[1]);
                    ibList.add(strArr[2]);
                }
            }
        }
        scheMe.setXyList(xyList);
        scheMe.setIbList(ibList);
        scheMe.setDbList(dbList);


        List<IbData> ibDatas = new ArrayList<>();

        for (int i = 0; i < Integer.parseInt(patTenr.getRePeat()); i++) {

            for (String ibStr : scheMe.getIbList()) {

                for (String str : dstStrList) {
                    if (str.contains("[" + ibStr + "]")) {
                        IbData ibData = new IbData();
                        List<String> ss = Arrays.asList(str.trim().split(System.getProperty("line.separator")));
                        ibData.setId(ss.get(1).split("=",2)[1]);
                        ibData.setOrder(ss.get(2).split("=",2)[1]);
                        ibData.setCustOmer(ss.get(3).split("=",2)[1]);
                        ibData.setBarCode(ss.get(4).split("=",2)[1]);
                        ibData.setRoTate(ss.get(5).split("=",2)[1]);
                        ibData.setWidTh(ss.get(6).split("=",2)[1].split("\\.",2)[0]);
                        ibData.setHeiGht(ss.get(7).split("=",2)[1].split("\\.",2)[0]);
                        ibData.setSpec(ss.get(8).split("=",2)[1]);
                        ibData.setRaCk(ss.get(9).split("=",2)[1]);
                        ibData.setNote1(ss.get(10).split("=",2)[1]);
                        ibData.setNote2(ss.get(11).split("=",2)[1]);
                        ibData.setNote3(ss.get(12).split("=",2)[1]);
                        ibData.setClaSsify(ss.get(13).split("=",2)[1]);
                        ibData.setCtiMes(ss.get(14).split("=",2)[1]);
                        ibDatas.add(ibData);
                    }
                }
            }
        }
        dstDat.setPatTenr(patTenr);
        dstDat.setScheMe(scheMe);
        dstDat.setIbData(ibDatas);
        this.dstData = dstDat;
    }

    @Override
    public String toString() {
        return "Dst{" +
                "dstData=" + dstData +
                '}';
    }
}
